package com.example.smart_hospital.service.user;

import com.example.smart_hospital.pojo.Mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.Random;
@Service
public class MailService {

    @Autowired
    private JavaMailSender javaMailSender;


    /**
     * 发送邮件
     * @param mail
     */
    public void sendSimpleMailMessage(Mail mail){
        System.out.println(mail);
        //封装simpleMailMessage
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(mail.getFrom());
        simpleMailMessage.setTo(mail.getTo());
        simpleMailMessage.setCc(mail.getCc());
        simpleMailMessage.setSubject(mail.getSubject());
        simpleMailMessage.setText(mail.getContent());
        javaMailSender.send(simpleMailMessage);//发送
    }

    /**
     * 随机验证码
     * @return
     */
    public String verificationCode(){
        Random yzm = new Random();                          //定义一个随机生成数技术，用来生成随机数
        //2，用String常用API-charAit生成验证码
        String yzm1 = "1234567890abcdefghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ";//定义一个String变量存放需要的数据，一共58位
        String yzm3 = "";//定义一个空的Atring变量用来接收生成的验证码
        for (int i = 0; i < 5; i++) {
            int a = yzm.nextInt(58);//随机生成0-57之间的数，提供索引位置
            yzm3+=yzm1.charAt(a);//用get 和提供的索引找到相应位置的数据给变量
        }
        System.out.println("用String常用API-charAit生成的验证码为:"+yzm3);
        return yzm3;
    }
}